# -*-coding=UTF-8-*-
import json
import traceback

from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt

from app.task.common.task_query import TaskManageDB
from app.task.common.task_add_edit_delete import TaskEditDB
from utils.logger import Md_Debug
from utils.response_utils import response_change

TaskManageDB = TaskManageDB()
TaskEditDB = TaskEditDB()


def public_view(request, func, session_content=False):
    data = dict()
    try:
        body = json.loads(request.body)
        if session_content == True:
            body["session_user_id"] = request.session.get("session_user_id")
            body["session_user_name"] = request.session.get("name")
            body["session_company_post"] = request.session.get("company_post")
            body["session_department"] = request.session.get("department")
            if body["session_user_id"] == None:
                data = {"response_msg": "params is error", "response_code": ImportError.IMPORT_CODE_EXECUTE_FAILED}
                raise ("session is error")
        data = func(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(data)
    return response_change(response)

@csrf_exempt
def task_list(request):
    """任务列表"""
    return public_view(request,TaskManageDB.task_list,True)

@csrf_exempt
def add_task(request):
    """添加任务"""
    data = {}
    try:
        body = json.loads(request.body)
        data = TaskEditDB.add_task(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(data)
    return response_change(response)

@csrf_exempt
def del_task(request):
    """删除任务"""
    try:
        body = json.loads(request.body)
        data = TaskEditDB.del_task(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(data)
    return response_change(response)

@csrf_exempt
def task_info(request):
    """任务信息"""
    try:
        body = json.loads(request.body)
        data = TaskManageDB.task_info(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(data)
    return response_change(response)

@csrf_exempt
def edit_task(request):
    """编辑任务"""
    try:
        body = json.loads(request.body)
        data = TaskEditDB.edit_task(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(data)
    return response_change(response)

@csrf_exempt
def task_member_list(request):
    """任务团队列表"""
    try:
        body = json.loads(request.body)
        data = TaskManageDB.task_member_list(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(data)
    return response_change(response)

@csrf_exempt
def add_task_member(request):
    """添加任务团队"""
    try:
        body = json.loads(request.body)
        data = TaskEditDB.add_task_member(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(data)
    return response_change(response)

@csrf_exempt
def del_task_member(request):
    """删除任务团队"""
    try:
        body = json.loads(request.body)
        data = TaskEditDB.del_task_member(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(data)
    return response_change(response)

@csrf_exempt
def task_member_info(request):
    """任务团队信息"""
    try:
        body = json.loads(request.body)
        data = TaskManageDB.task_member_info(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(data)
    return response_change(response)

@csrf_exempt
def edit_task_member(request):
    """编辑任务团队"""
    try:
        body = json.loads(request.body)
        data = TaskEditDB.edit_task_member(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(data)
    return response_change(response)

@csrf_exempt
def task_member_edit_user(request):
    """添加修改任务团队成员"""
    try:
        body = json.loads(request.body)
        data = TaskEditDB.task_member_edit_user(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(data)
    return response_change(response)

@csrf_exempt
def task_member_del_user(request):
    """添加修改任务团队成员"""
    try:
        body = json.loads(request.body)
        data = TaskEditDB.task_member_del_user(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(data)
    return response_change(response)

@csrf_exempt
def task_leader_list(request):
    return public_view(request, TaskManageDB.task_leader_list,True)

@csrf_exempt
def project_task_leader_list(request):
    return public_view(request,TaskManageDB.project_task_leader_list)

@csrf_exempt
def task_info_person(request):
    return public_view(request, TaskManageDB.task_info_person)

@csrf_exempt
def task_empty_person(request):
    return public_view(request, TaskManageDB.task_empty_person)

@csrf_exempt
def task_edit_leader(request):
    return public_view(request, TaskEditDB.task_edit_leader)

@csrf_exempt
def task_member_add_user(request):
    return public_view(request, TaskEditDB.task_member_add_user)

# PROJECT_TASK_LIST
@csrf_exempt
def project_task_list(request):
    return public_view(request, TaskManageDB.project_task_list)

@csrf_exempt
def task_over(request):
    return public_view(request, TaskEditDB.task_over)

@csrf_exempt
def task_search(request):
    return public_view(request, TaskManageDB.task_search,True)